import axios from 'axios';
import { getToken } from './auth';

const service = axios.create({
  baseURL: '/api',
  timeout: 5000,
});

service.interceptors.request.use(
  config => {
    const token = getToken();
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => response,
  error => {
    return Promise.reject(error);
  }
);

export default service;
